<template>
  <div>
    <!-- 编辑昵称 -->
    <!-- 导航栏 -->
    <van-nav-bar title="编辑昵称" left-text="返回" right-text="确认" left-arrow @click-left="$emit('left-click')" @click-right="onSeve
      " />
    <!-- 输入框 -->
    <van-field v-model="name" type="textarea" maxlength="8" placeholder="请输入昵称" show-word-limit />
  </div>
</template>

<script>
// 修改用户昵称请求
import { setUserProfile } from '@/api/user.js'
export default {
  data () {
    return {
      // 输入框内容
      name: this.value
    }
  },
  props: {
    // 父传子昵称名
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    // 点击保存修改用户昵称
    async onSeve () {
      if (this.name.trim() === 0) return this.$toast('请输入昵称')
      try {
        await setUserProfile({
          name: this.name
        })
        this.$toast('修改昵称成功')
        this.$emit('left-click')
        this.$emit('input', this.name)
      } catch (err) {
        console.log(err)
        if (err.response && err.response.status === 409) return this.$toast('用户名已存在')
        this.$toast('修改昵称失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
